﻿/**
 * 远光ECP平台3.0版本 http://www.ygsoft.com/ Grace [Gris Application Cloud-Computing
 * Extension]
 * 
 * 构建文本输入验证框控件类
 * 
 * 变更版本：
 * zhengxianwei@ygsoft.com 2016-05-03 创建
 */
define(["ecp.utils","ecp.component.popover", "ecp.utils.i18n"],function(utils,popover,locale){
	/**
	 * 默认参数.
	 */
	var defaultOptions = {
		popoverOption: {
			trigger : 'manual'
		}
	};
	var _locale = locale.validateMessage;
	/**
	 * 声明ValidateBox对象
	 */
	var ValidateBox = function(control,options){
		if(!control || control.length === 0){return;}
		this.init(control,options);
	}
	/**
	 * 通过原型定义相关方法，比如渲染、赋值等等.
	 */
	ValidateBox.prototype = {
		/**
	     * 初始化控件
	     * 
	     * @param {Selector} control 控件
	     * @option {Object} option 配置参数
	     */
		init : function(control, option){
	    	if(!control || control.length === 0){return;}
			this.control = control;
			var dOpt = control.attr("data-option");
			if(dOpt && dOpt.length !== 0){
				dOpt = $.parseJSON(dOpt);
			}else{
				dOpt = {};
			}
			this.option = $.extend(true,{},defaultOptions,option,dOpt);
			var rules = this.option.rules;
			if(!this.checkRules(rules)){
				return;
			}
			this.reg = new RegExp(this.option.rules.expression);
			//校验不通过的提示信息
			this.option.popoverOption.content = (rules.name && _locale[rules.name]) || rules.formatsl;
			this.option.popoverOption.placement = function(ctx, element){
				var $element = $(element),
					$winWidth = $(window).width(),
					eleWidth = parseInt($element.css("width"), 10),
					left = $element.offset().left;
				if ($winWidth - left - eleWidth < eleWidth ) {
					return 'top';
				} else {
					$(ctx).css("width", "200px");
					return "right";
				}
			};
			this.render();
		},
		/**
		 * 渲染控件。
		 */
		render : function(){		
			var that = this;
			that.control.popover(this.option.popoverOption);
			that.control.on("keydown keyup focus mouseover", function(){
				if(that.checkExpression(that.control.val())){
					that.isShown() && that.control.popover('hide');
					that.control.removeClass("invalidate-msg");
				} else {
					that.isShown() || that.control.popover('show');
					that.control.addClass("invalidate-msg");
				}
			}).on("blur mouseout", function(){
				if(that.isShown()){
					that.control.popover('hide');
					that.control.addClass("invalidate-msg");
				}
			});
		},
		/**
    	 * 设置样式.
    	 * @example 1
    	 * validateBox.setStyle("backgroundColor","red");
    	 * @example 2
    	 * validateBox.setStyle({"backgroundColor":"red"});
    	 * 
    	 * @param {String | Object} styNm 为字符串时表示样式名，为object时是样式对象
    	 * @param {String} val 样式的值
    	 * 
    	 */
    	setStyle : function(styNm, val){
    		if(!styNm){return;}
    		var res = {};
    		if(typeof styNm === "string"){
    			res[styNm] = val;
    		}else if(typeof styNm === "object"){
    			res = styNm;
    		}
    		if(!$.isEmptyObject(res)){
    			this.control.css(res);
    		}
    	},
				
		value : function(value){
			var me = this,
				ct = me.control;
			if(arguments.length){
				ct.data("value",value);//负责往数据模型中设置值
				me.initValue(value);//负责控件值的显示
			} else {
				return ct.data("value");
			}
		},
		initValue : function(value){
			if(!utils.notEmpty(value)){
				this.control.removeClass("invalidate-msg");
			}
			this.getControl().val(value);
			this.updateTitle(value);
		},
		getControl : function(){
			return this.control;
		},
		bind : function(event, handle){
			this.control.bind(event, handle);
		},
		unbind : function(event, handle){
			this.control.unbind(event, handle);
		},
		/**
		 * 更新提示
		 */
		updateTitle: function(v){
			var input = this.control;
			if(input && input.is("input") && !input.is(":password")){
				var t, vt;
				if(!arguments.length){
					v = input.val();
				}
				vt = typeof v;
				if( vt=== "undefined"){
					t = "";
				} else if(vt === "string"){
					t = v;
				} else if($.isArray(v)){
					t = v.join(",")
				} else {
					t = "";
				}
				t = t.length>200 ? t.substring(0,197).concat("...") : t;
				input.attr("title",t);
			}
		},
		resizeWidth : function(width){
			if(typeof(width) === "string"){
				(width.indexOf("px") < 0) && (width = width + "px");
			}else if(typeof(width) !== "number"){
				width = "";
			}
			this.control.css("width",width);
		},
		/**
		 * 检查checkRules是否符合规则
		 */
		checkRules: function(rules){
			//允许不存在rules.name
			if (rules && rules.expression && rules.formatsl) {
				return true;
			} else {
				return false;
			}
		},
		/**
		 * popover是否已经显示
		 */
		isShown: function(){
			return !!this.control.next("div[class*='popover']").length;
		},
		/**
		 * 正则匹配
		 */
		checkExpression: function(value){
			if(value === ''){
				return true;
			} else {
				if(this.reg.test(value)){
					return true;
				} else {
					return false;
				}
			}
		}
	};
	(function($) {
		var old = $.fn.validateBox;
		$.fn.validateBox = function(options){
			var me = this;
			options = options || {};
			var type = typeof(options);
			//如果第一个参数传入的是对象或空则初始化空件，如果传入的是字符串，则认为是调用里面的内部方法。
			if (type === "object") {
				var numBoxs = [];
				this.each(function() {
					var instanceOptions = $.extend({}, options, true);
					var instance = new ValidateBox($(this), instanceOptions);
					this.validateBox = instance;
					numBoxs.push(instance);
				});
				if(numBoxs.length === 1){
					numBoxs = numBoxs[0];
				}
				return numBoxs;
			} else if (type === "string") {
				var args = Array.prototype.slice.call(arguments, 1);
				if (ValidateBox.prototype[options]) {
					return ValidateBox.prototype[options].call(args);
				}
			} else if (options === true) {
				return me[0] && me[0].validateBox;
			}
		};
		$.fn.validateBox.Constructor = ValidateBox;
		$.fn.validateBox.noConflict = function() {
			$.fn.validateBox = old;
			return this;
		};
	}(jQuery));
	return ValidateBox.prototype;
	
})